/**
 * Created by CZ on 2017/5/4.
 */
module.exports = function (RequestService,$localStorage,commonConfig) {
    return {
        restrict: 'EA',
        templateUrl: 'views/model/lunboModel.html',
        scope: {
            myId: '@'
        },
        link: function (scope, element, attr) {
            scope.imgApi = commonConfig.$webroot+commonConfig.imgApi+'?access_token='+$localStorage.get('cloud').token+'&uuid=';
            // var promise = readJSON.query();
            // promise.then(function (data) {
            //     console.log(data);
            //     scope.images = data;
            // });
            // scope.flag = false;
            //获取token
            var token = $localStorage.get('cloud').token;
            RequestService.Request(commonConfig.cloudApi+'cloud/appphoto?appId='+scope.myId+'&access_token='+token,'GET').then(function(response){
                console.log(response);
                scope.images = response;
            },function(error){
                console.log(error);
            });
            var step = 0;
            setInterval(function () {
                // console.log(scope.myId);
                angular.element(document.querySelectorAll('.sm-img')).css({"opacity": 0.3});
                element.find("li").css({"display": "none", "opacity": 0});
                step++;
                step = step % scope.images.length;
                angular.element(document.querySelectorAll('.sm-img ')).eq(step).css({"opacity": 1});
                element.find("li").eq(step).css({"display": "block", "opacity": 1});
            }, 5000);
            scope.click = function ($event) {
                angular.element(document.querySelectorAll('.sm-img')).css({"opacity": 0.3});
                angular.element($event.target).css({"opacity": 1});
                angular.forEach(document.querySelectorAll('.sm-img'), function (val, i) {
                    if (val == $event.target) {
                        element.find("li").css({"display": "none", "opacity": 0});
                        element.find("li").eq(i).css({"display": "block", "opacity": 1});
                    }
                });
            }
        }
    }
};